$(document).ready(function () {
    $(function () {
        $('#collapsed1').collapse('show')
    });

    var widowheight = $(window).height() - 50;
    $("body").css("height", widowheight);
// 拿到商品分类
    $.ajax({
        url: "http://" + urla + "/GetGoodTypeServlet",
        //url: "./json/categories/parent/0/list.json",
        // resultJson{
        //     data:[goodType],
        // }
        type: "GET",
        dataType: 'JSON',
        success: function (result) {
            console.log(result);
            addChildCategory(result[0].type_id);
                $.each(result, function (x, obj) {
                    var $op = $("<option class='opParent' value='" + obj.type_id + "'>" + obj.type_name + "</option>");
                    $("#selParentId").append($op);
                });
            $('#selParentId').change(function () {
                addChildCategory($(this).val());
                /*console.log($(this).val());*/

            });

            /*  $('#selChlidId').change(function () {
             console.log($(this).val());
             });
             */
        },
        error: function (data) {
            console.log("X");
            console.log(data);
        }
    });

    function addChildCategory(id) {
        $.ajax({
            url: "http://" + urla + "/GetChildTypeServlet",
            //url: "./json/categories/parent/0/list.json",
            type: "POST",
            data:"parentId=" + id,
            dataType: 'JSON',
            success: function (result) {
                $("#selChlidId").empty();

                $.each(result, function (x, obj) {
                    var $op2 = $("<option class='opChild' value='" + obj.type_id + "'>" + obj.type_name + "</option>");
                    $("#selChlidId").append($op2);
                })
                /*console.log($('#selChlidId').val());*/
            },
            error: function (data) {
                console.log("X");
                console.log(data);
            }
        });
    }

    $(".img-thumbnail").css("display", "none");
    var imgnum = 1;
    var uriStr = '';
    //上传图片
    $("#uploadImgBtn").click(function () {
        var formData = new FormData();
        formData.append('upload_file', $('#file')[0].files[0]);
        $.ajax({
            url: "http://" + urla + "/upLoadGoodImgServlet",
            type: 'POST',
            cache: false,
            data: formData,
            dataType:"json",
            processData: false,
            contentType: false,
            success: function (data) {
                console.log(typeof data);
                $("#img" + imgnum).css("display", "block");
                $("#img" + imgnum).attr("src", data.url);
                uriStr += data.url + ',';
                imgnum++;
                if (imgnum>5){
                   /* uriStr=uriStr.substring(0,uriStr.length-1);*/
                    $("#uploadImgBtn").attr("disabled", "disabled");
                    console.log(uriStr);
                }
            },
            error: function (data) {
                console.log("X");
                console.log(data);
            }
        });
    });

    var successModal=$('#successModal');
    var unknownModal=$('#unknownModal');



    $("#upload").on("click", function () {
        const name = $("#inputName").val();
        const subtitle = $("#inputSubtitle").val();
        const price = $("#inputPrice").val();
        const stock = $("#inputStock").val();
        const parentTypeId = $("#selParentId").val()
        const childTypeId = $("#selChlidId").val();
        const detail = $("#inputDetail").val();
        const subImages = uriStr.substring(0,uriStr.length-1);

        console.log(name);
        const newGoodInfo = {
            "name":name,
            "subtitle":subtitle,
            "price":price,
            "stock":stock,
            "parentTypeId":parentTypeId,
            "childTypeId":childTypeId,
            "detail":detail,
            "subImages":subImages
        }

        $.ajax({
            contentType:"application/x-www-form-urlencoded;charset=UTF-8",
            type: "Post",
            cache: true,
            url: "http://" + urla + "/AddGoodServlet",
            data: newGoodInfo,
            dataType:"json",
            error:function (error) {
                console.log(error)
                unknownModal.modal('show');
                unknownModal.css("padding-right","0");
                $("#unknownModal .alert").text("上传信息不能为空！");
                unknownModal.on('hide.bs.modal', function () {
                    location.reload();
                })
            },
            success: function (data) {
                console.log(typeof data);
                console.log(data.msg);
                if (data.status==1){
                    unknownModal.modal('show');
                    unknownModal.css("padding-right","0");
                    $("#unknownModal .alert").text(data.msg+"请重试");
                    unknownModal.on('hide.bs.modal', function () {
                        location.reload();
                    })
                }else {
                    successModal.modal('show');
                    successModal.css("padding-right","0");
                    successModal.on('hide.bs.modal', function () {
                        location.reload();
                    })
                }


            }
        });
    });



});